<?php
/* 
 * Project: oCoder Framework
 * File: /models/user.php
 * Purpose: model for the user controller.
 * Author: Trung Hoang
 */

class DiaDiemDuLichModel extends BaseModel
{
    //data passed to the user index view
    public function index($para1)
    {   
    	// get all users    

    	$diadiemdulichs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'id DESC', $where = '', $cols = '*', $limit = ($para1-1)*ROWOFPAGE.','.ROWOFPAGE);
        // get all users
        // $articles = $this->find_all($table = 'articles', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        
        $sodiadiem = $this->count($table = 'dd_diadiemdulich');

        $this->viewModel->set("pageTitle","Danh sách địa điểm du lịch");
        $this->viewModel->set("diadiemdulichs",$diadiemdulichs);
        $this->viewModel->set("sodiadiem",$sodiadiem);
        
        $this->viewModel->set("tranghientai",$para1);
        return $this->viewModel;
    }
    public function place($alias)
    {   
        // get all users    

        $diadiemdulich = $this->find($table='view_dd_diadiemdulich', $orderby = 'id DESC', $where = 'Alias like "'.$alias.'"', $cols = '*', $limit = '');
        // echo '<pre>'; var_dump($diadiemdulich); echo '</pre>';die;
        // get all users
        $tukhoadiadiem = $diadiemdulich ->TuKhoaDiaDiem;
        // update view place
        $soluongtruycap = $diadiemdulich ->TongSoLuotXem;
         $data1 = array(
                        'TongSoLuotXem' => $soluongtruycap+1, 
                                        
                    );

        $vido = $diadiemdulich ->ViDo;
        $kinhdo = $diadiemdulich ->KinhDo;
        $tinhthanh = $diadiemdulich ->TinhThanh;
        // $articles = $this->find_all($table = 'articles', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $hinhanhs = $this->find_all($table = 'dd_media', $orderby = 'id DESC', $where = 'TuKhoaDiaDiem like "'.$tukhoadiadiem.'" and Loai not like "video"', $cols = '*', $limit = '');
        $videos = $this->find_all($table = 'dd_media', $orderby = 'id DESC', $where = 'TuKhoaDiaDiem like "'.$tukhoadiadiem.'" and Loai like "video"', $cols = '*', $limit = '');
        $baiviets = $this->find_all($table = 'bv_baiviet', $orderby = 'id DESC', $where = 'TuKhoaDiaDiem like "'.$tukhoadiadiem.'"', $cols = '*', $limit = '');
        
        $diadiemgannhats = $this->find_all($table = 'dd_diadiemdulich', $orderby = 'distance', $where = 'TuKhoaDiaDiem not like "'.$tukhoadiadiem.'"', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
            sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
            cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
            pi()/180))))*180/pi())*60*1.1515*1.609344
        ) as distance', $limit = '5');
        $diadiemmuonxems = $this->find_all($table = 'dd_diadiemdulich', $orderby = 'SoLuotXem DESC', $where = 'TinhThanh like "'.$tinhthanh.'"', $cols = '*', $limit = '5');
        // echo '<pre>';var_dump($diadiemgannhats);echo '</pre>';die;
        $this->viewModel->set("diadiemdulich",$diadiemdulich);        
        $this->viewModel->set("hinhanhs",$hinhanhs);
        $this->viewModel->set("videos",$videos);
        $this->viewModel->set("baiviets",$baiviets);
        $this->viewModel->set("diadiemgannhats",$diadiemgannhats);
        $this->viewModel->set("diadiemmuonxems",$diadiemmuonxems);
        $this->viewModel->set("pageTitle",$diadiemdulich ->TenDiem);
        $this->viewModel->set("baiviets",$baiviets);

        return $this->viewModel;
    }
    public function tracuu($kinhdo, $vido)
    {         
        $diadiemdulichs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $tinhthanhs = $this->find_all($table = 'view_dd_sodiadiemtheotinh', $orderby = 'IdTinhThanh ASC', $where = '', $cols = '*', $limit = '');
        if ($kinhdo !='' && $vido!=''){
            $diadiemgannhats = $this->find_all($table = 'dd_diadiemdulich', $orderby = 'distance', $where = 'TuKhoaDiaDiem not like "'.$tukhoadiadiem.'"', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
                sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
                cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
                pi()/180))))*180/pi())*60*1.1515*1.609344
            ) as distance', $limit = '5');
            $this->viewModel->set("kinhdo",$kinhdo);
            $this->viewModel->set("vido",$vido);
            $this->viewModel->set("diadiemgannhats",$diadiemgannhats);
        }
        $this->viewModel->set("diadiemdulichs",$diadiemdulichs);
        $this->viewModel->set("tinhthanhs",$tinhthanhs);
        
        return $this->viewModel;
    }
    public function search($where_search, $keyword)
    { 
        $ketquatimkiems = $this->find_all($table = 'dd_diadiemdulich', $orderby = 'id DESC', $where = $where_search, $cols = '*', $limit = '');
        $this->viewModel->set("ketquatimkiems",$ketquatimkiems);
        $this->viewModel->set("keyword",$keyword);
        return $this->viewModel;
    }
    public function province($alias)
    { 
        $tinhthanh = $this->find($table='view_dd_tinhthanh', $orderby = '', $where = 'DuongDan like "'.$alias.'"', $cols = '*', $limit = '');
        // echo '<pre>';var_dump($tinhthanh); echo '</pre>'; die;
        $tukhoa = $tinhthanh->TuKhoa;
        $vido = $tinhthanh->ViDo;
        $kinhdo = $tinhthanh->KinhDo;
        $diadiemthamquans = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and (LoaiDiaDiem like "dia-diem-tham-quan" or LoaiDiaDiem like "bai-tam" )', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
            sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
            cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
            pi()/180))))*180/pi())*60*1.1515*1.609344
        ) as distance', $limit = '5');

        $diadiemluutrus = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and (LoaiDiaDiem like "khach-san" or LoaiDiaDiem like "resort" or LoaiDiaDiem like "nha-nghi")', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
            sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
            cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
            pi()/180))))*180/pi())*60*1.1515*1.609344
        ) as distance', $limit = '5');
        $diadiemanuongs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and (LoaiDiaDiem like "nha-hang" or LoaiDiaDiem like "quan-an" or LoaiDiaDiem like "quan-cafe" or LoaiDiaDiem like "quan-bar" )', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
            sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
            cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
            pi()/180))))*180/pi())*60*1.1515*1.609344
        ) as distance', $limit = '5');
        $diadiemkhacs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and (LoaiDiaDiem like "truong-hoc" or LoaiDiaDiem like "quan-an" or LoaiDiaDiem like "quan-cafe" or LoaiDiaDiem like "quan-bar" )', $cols = '*,(((acos(sin(('.$vido.'*pi()/180)) * 
            sin((ViDo*pi()/180))+cos(('.$vido.'*pi()/180)) * 
            cos((ViDo*pi()/180)) * cos((('.$kinhdo.'- KinhDo)* 
            pi()/180))))*180/pi())*60*1.1515*1.609344
        ) as distance', $limit = '5');

        // data map 
        $diadiemdulichs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = '', $where = 'TinhThanh like "'.$tukhoa.'"', $cols = '*', $limit = '');
        $baiviets = $this->find_all($table = 'bv_baiviet', $orderby = '', $where = 'TuKhoaTinhThanh like "'.$tukhoa.'"', $cols = '*', $limit = '4');
        // $khachsans = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "khach-san"', $cols = '*', $limit = '');
        // $resorts = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "resort"', $cols = '*', $limit = '');
        // $nhahangs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "nha-hang"', $cols = '*', $limit = '');
        // $quanbars = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "quan-bar"', $cols = '*', $limit = '');
        // $quancafes = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "quan-cafe"', $cols = '*', $limit = '');
        // $quanans = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "quan-an"', $cols = '*', $limit = '');
        // $baibiens = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "bai-tam"', $cols = '*', $limit = '');
        // $truonghocs = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "truong-hoc"', $cols = '*', $limit = '');
        // $nhanghis = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "nha-nghi"', $cols = '*', $limit = '');
        // $khuthuongmais = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "khu-thuong-mai"', $cols = '*', $limit = '');
        // $benxes = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "ben-xe"', $cols = '*', $limit = '');
        // $chos = $this->find_all($table = 'view_dd_diadiemdulich', $orderby = 'distance', $where = 'TinhThanh like "'.$tukhoa.'" and LoaiDiaDiem like "cho"', $cols = '*', $limit = '');


        $this->viewModel->set("tinhthanh",$tinhthanh);
        $this->viewModel->set("diadiemthamquans",$diadiemthamquans);
        $this->viewModel->set("diadiemluutrus",$diadiemluutrus);
        $this->viewModel->set("diadiemdulichs",$diadiemdulichs);
        $this->viewModel->set("diadiemanuongs",$diadiemanuongs);
        $this->viewModel->set("diadiemkhacs",$diadiemkhacs);
        $this->viewModel->set("baiviets",$baiviets);
        // $this->viewModel->set("khachsans",$khachsans);
        // $this->viewModel->set("resorts",$resorts);
        // $this->viewModel->set("nhahangs",$nhahangs);        
        // $this->viewModel->set("quanbars",$quanbars);
        // $this->viewModel->set("quancafes",$quancafes);
        // $this->viewModel->set("quanans",$quanans);
        // $this->viewModel->set("baibiens",$baibiens);
        // $this->viewModel->set("truonghocs",$truonghocs);
        // $this->viewModel->set("nhanghis",$nhanghis);
        // $this->viewModel->set("khuthuongmais",$khuthuongmais);
        // $this->viewModel->set("benxes",$benxes);
        // $this->viewModel->set("chos",$chos);
        return $this->viewModel;

    }
    //create model
    public function create($data)
    {   
        //create new item when have data
    	if(!empty($data)){
            //return id of item after insert to db
    		return $this->save($table = 'dd_diadiemdulich', $data = $data, $where = '');
    	}
        $tinhthanhs = $this->find_all($table = 'tinhthanh', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $loaidiadiems = $this->find_all($table = 'dd_loaidiadiem', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        
        $this->viewModel->set("tinhthanhs",$tinhthanhs);       
        $this->viewModel->set("loaidiadiems",$loaidiadiems);       
        //param var for view
        $this->viewModel->set("pageTitle","Thêm địa điểm mới");

        return $this->viewModel;
    }
    public function add_media($data)
    {   
        //create new item when have data
        if(!empty($data)){
            //return id of item after insert to db
            return $this->save($table = 'dd_media', $data = $data, $where = '');
        }
                  
        return $this->viewModel;
    }
    
    //update model
    public function edit($id, $data)
    {
        //update item when have data
        if(!empty($data)){
            //return bool
            return $this->save($table = 'dd_diadiemdulich', $data = $data, $where = 'id='.$id);
        }

        //get item detail by id
        $diadiemdulich = $this->find($table='dd_diadiemdulich', $orderby = 'id DESC', $where = 'id='.$id, $cols = '*', $limit = '');
        $tukhoadiadiem = $diadiemdulich -> TuKhoaDiaDiem;
        // get media
        $hinhanhs = $this->find_all($table = 'dd_media', $orderby = 'id DESC', $where = 'TuKhoaDiaDiem like "'.$tukhoadiadiem.'" and Loai not like "video"', $cols = '*', $limit = '');
        $videos = $this->find_all($table = 'dd_media', $orderby = 'id DESC', $where = 'TuKhoaDiaDiem like "'.$tukhoadiadiem.'" and Loai like "video"', $cols = '*', $limit = '');
        //get tinhthanh + loaidiadiem
        $tinhthanhs = $this->find_all($table = 'tinhthanh', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $loaidiadiems = $this->find_all($table = 'dd_loaidiadiem', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
        $this->viewModel->set("tinhthanhs",$tinhthanhs);       
        $this->viewModel->set("loaidiadiems",$loaidiadiems);       
        $this->viewModel->set("hinhanhs",$hinhanhs);       
        $this->viewModel->set("videos",$videos);       
        //param var for view
        $this->viewModel->set("pageTitle","Chỉnh sửa địa điểm");
        $this->viewModel->set("diadiemdulich",$diadiemdulich);
        return $this->viewModel;
    }
    public function update_view_province($alias)
    {
        //update item when have data
        if(!empty($alias)){
            //return bool
            return $this->update_view($table = 'tinhthanh',$where = 'DuongDan like "'.$alias.'"');
        }
    }
    public function update_view_place($alias)
    {
        //update item when have data
        if(!empty($alias)){
            //return bool
            return $this->update_view($table = 'dd_diadiemdulich',$where = 'Alias like "'.$alias.'"');
        }
    }
}

?>